/****************************************************************************
 * 2025.7 GRAVEROBBER-PC
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvival
{
	public partial class AchievementController : UIElement
	{
		ResLoader _resLoader = ResLoader.Allocate();
		private SpriteAtlas _iconAtlas;
		private void Awake()
		{
			float originLocalPosy = AchievementItem.LocalPositionY();
			_resLoader.Add2Load<SpriteAtlas>("Icon");
			_resLoader.LoadAsync(() =>
			{
				_iconAtlas = _resLoader.LoadSync<SpriteAtlas>("Icon");
			});
			//_iconAtlas = _resLoader.LoadSync<SpriteAtlas>("icon");
			AchievementSystem.OnAchievementUnlocked.Register(item =>
			{
				Title.text = $"<b>成就{item.Name}达成！</b>";
				Description.text = item.Description;
				Icon.sprite = _iconAtlas.GetSprite(item.IconName);
				AchievementItem.Show();
				AchievementItem.LocalPositionY(-200);
				AudioKit.PlaySound("Achievement");
				ActionKit.Sequence()
					.Lerp(-200, originLocalPosy, 0.3f, y => { AchievementItem.LocalPositionY(y); })
					.Delay(2)
					.Lerp(originLocalPosy, -200, 0.3f, y => { AchievementItem.LocalPositionY(y); },
						() => { AchievementItem.Hide(); })
					.Start(this);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnBeforeDestroy()
		{
			_resLoader.Recycle2Cache();
			_resLoader = null;
		}
	}
}